package cn.lyz.craft.web.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.lyz.craft.common.R;
import cn.lyz.craft.service.entity.Role;
import cn.lyz.craft.service.service.RoleService;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/roles")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    @GetMapping
    public R<?> getAllRoles() {
        return R.success(roleService.findAll());
    }

    @GetMapping("/{id}")
    public R<Role> getRoleById(@PathVariable Long id) {
        return R.success(roleService.findById(id));
    }

    @PostMapping
    public R<Role> createRole(@RequestBody Role role) {
        return R.success(roleService.save(role));
    }

    @PutMapping("/{id}")
    public R<Role> updateRole(@PathVariable Long id, @RequestBody Role role) {
        role.setId(id);
        return R.success(roleService.save(role));
    }

    @DeleteMapping("/{id}")
    public R<?> deleteRole(@PathVariable Long id) {
        roleService.deleteById(id);
        return R.success();
    }
}